﻿/*版权声明
 * 
 * 本 河北大学管理学院信息管理系网站 由
 * 河北大学 信管团队 开发
 * 如有任何疑问请联系开发人员
 * 
 * 项目开发者：朱胜贤、孙旭峰、水亚亚、张文元、项玥、吕芳、吴凡、谢静、李亚慧
 * 
 * 如有任何疑问请联系：河北大学信管团队
 * QQ：196396337
 * Email:zagelover@163.com
 * HomePage：http://sxgzs.com/
 * 
 * CopyRight (c) 河北大学信管团队 2010~2020
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Admin_Download_Type_Manage : System.Web.UI.Page
{
    /// <summary>
    /// 页面加载
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["Uid"] == null)
        {
            Response.Redirect("AD_Login.aspx");
            return;
        }
        if (!IsPostBack)
        {
            string downType = Request.QueryString["downType"].ToString().Trim();
            if (downType != "")
            {
                btnCancel.Text = "取 消";
                Bind_Txt(downType);
            }
            else
            {
                btnCancel.Text = "清 空";
            }
            Bing_GV();
        }
    }
    /// <summary>
    /// GridView绑定下载列表
    /// </summary>
    public void Bing_GV()
    {
        string sql = "select * from Download_Type";
        GV.DataSource = DataAccess.GetDataTable(sql, null);
        GV.DataKeyNames = new string[] { "typeName" };
        GV.DataBind();
    }
    /// <summary>
    /// 为GridView行添加属性
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void GV_RowCreated(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Attributes.Add("onmouseover", "c=style.backgroundColor;this.style.backgroundColor='#6699ff'");
            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");
        }
    }
    /// <summary>
    /// 响应GridView行删除事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void GV_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        string sql = "delete from Download_Type where typeName=@typeName";
        SqlParameter[] para = { new SqlParameter("@typeName",SqlDbType.NVarChar,50) };
        para[0].Value = GV.DataKeys[e.RowIndex][0].ToString();
        if (DataAccess.GetCount(sql, para) > 0)
        {
            lblMessage.Text = "删除数据成功！";
            Bing_GV();
        }
        else
        {
            lblMessage.Text = "删除数据失败！";
        }
    }
    /// <summary>
    /// 添加下载类型
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnOK_Click(object sender, EventArgs e)
    {
        
            if (lblType.Text == "")
            {
                string sql2 = "insert into Download_Type(typeName,typeIntro) values(@typeName,@typeIntro)";
                SqlParameter[] para1 = {
                                     new SqlParameter("@typeName",SqlDbType.NVarChar,50),
                                     new SqlParameter("@typeIntro",SqlDbType.NVarChar,50)
                                 };
                para1[0].Value = txtName.Text;
                para1[1].Value = txtInto.Text;
                if (DataAccess.GetCount(sql2, para1) > 0)
                {
                    lblMessage.Text = "添加信息成功！";
                    Bing_GV();
                    txtInto.Text = "";
                    txtName.Text = "";
                    //Response.Write("<script>alert('添加信息成功！');location.href='Download_Type_Manage.aspx?downType=';</script>");
                }
                else
                {
                    lblMessage.Text = "添加数据失败！";
                }
            }
            else
            {
                string sql3 = "update Download_Type set typeName=@typeName,typeIntro=@typeIntro where typeName=@downType";
                SqlParameter[] para2 = {
                                   new SqlParameter("@typeName",SqlDbType.NVarChar,50),
                                   new SqlParameter("@typeIntro",SqlDbType.NVarChar,50),
                                   new SqlParameter("@downType",SqlDbType.NVarChar,50)
                                   };
                para2[0].Value = txtName.Text;
                para2[1].Value = txtInto.Text;
                para2[2].Value = lblType.Text;
                if (DataAccess.GetCount(sql3, para2) > 0)
                {
                    lblMessage.Text = "修改信息成功！";
                    txtInto.Text = "";
                    txtName.Text = "";
                    lblType.Text = "";
                    Bing_GV();
                    //Response.Write("<script>alert('修改信息成功！');location.href='Download_Type_Manage.aspx?downType=';</script>");
                }
                else
                {
                    lblMessage.Text = "修改数据失败！";
                }
            }
        }

    /// <summary>
    /// 通过btnCancel的Text值来判断不同的动作
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        if (btnCancel.Text.Trim() == "取 消")
        {
            Response.Redirect("Download_Type_Manage.aspx?downType=");
        }
        else if(btnCancel.Text.Trim() == "清 空")
        {
            txtName.Text="";
            txtInto.Text="";
        }
    }
    private void Bind_Txt(string downType)
    {
        string sql1 = "select * from Download_Type where typeName=@typeName";
        SqlParameter[] para = {new SqlParameter("@typeName",SqlDbType.NVarChar,50) };
        para[0].Value = downType;
        SqlDataReader DR = DataAccess.GetReader(sql1, para);
        if (DR.HasRows)
        {
            while (DR.Read())
            {
                ViewState["typeName"] = downType;
                txtName.Text = DR["typeName"].ToString();
                txtInto.Text = DR["typeIntro"].ToString();
                lblType.Text = DR["typeName"].ToString();
            }
        }
    }
}